﻿
/*******************************************************************************************
// ©2009 Microsoft Corporation.  This code is provided under the Microsoft Public License.
*******************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace ClientBilling
{
    public partial class Default : System.Web.UI.Page
    {
        private const string CALL_QUERY_STRING = "call";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (BilledCallSelected)
            {
                PlaceSelectedBilledCall();
            }
        }

        private bool BilledCallSelected
        {
            get
            {
                return Request.QueryString[CALL_QUERY_STRING] != null;
            }
        }

        private void PlaceSelectedBilledCall()
        {
            Case callCase = CrmSystem.GetCaseByNumber(Request.QueryString[CALL_QUERY_STRING]);

            if (callCase != null) // Check for a null in case someone altered the query string and no such case exists.
            {
                BilledCallPlacer placer = BilledCallManager.Instance.GetBilledCallPlacer();
                placer.PlaceCallForCase(callCase);
            }
        }

        protected void CleanupButton_Click(object sender, EventArgs e)
        {
            BilledCallManager.Instance.Cleanup();
        }

        protected void StartupButton_Click(object sender, EventArgs e)
        {
            BilledCallManager.Instance.Startup();
        }

        #region Repeater Data Binding

        protected void ClientRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Repeater caseRepeater = (e.Item.FindControl("CaseRepeater") as Repeater);
            Client client = (e.Item.DataItem as Client);

            if (caseRepeater != null && client != null)
            {
                caseRepeater.DataSource = client.Cases;
                caseRepeater.DataBind();
            }
        }

        protected void CaseRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ImageButton callButton = (e.Item.FindControl("CallButton") as ImageButton);
            LinkButton linkButton = (e.Item.FindControl("CaseLabel") as LinkButton);
            Case callCase = (e.Item.DataItem as Case);

            if (callButton != null && callCase != null)
            {
                LinkCallButtonToCase(callButton, callCase);
                LinkCallButtonToCase(linkButton, callCase);
            }
        }

        private void LinkCallButtonToCase(IButtonControl callButton, Case callCase)
        {
            callButton.PostBackUrl = string.Format("{0}?{1}={2}", Request.Path, CALL_QUERY_STRING, callCase.Number);
        }

        #endregion
    }
}
